package com.hysf.project1;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
@ComponentScan("com.hysf")
@PropertySource(value = "classpath:db.properties")
@EnableAspectJAutoProxy
public class AppConfig {
	@Value ("${driver}")
	private String driver;
	@Value ("${url}")
	private String url;
	@Value ("${name}")
	private String username;
	@Value ("${password}")
	private String password;
	@Bean(initMethod = "init", destroyMethod = "close")
	public DruidDataSource dataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(driver);
		dataSource.setUrl(url);
		dataSource.setUsername(username);
		dataSource.setPassword(password);
		dataSource.setInitialSize(10); // 初始化连接数
		dataSource.setMaxActive(20); // 最大连接数
		dataSource.setMinIdle(10); // 最小连接数
		dataSource.setMaxWait(60000); // 最大等待时间
		return dataSource;
	}
	
	@Bean
	public JdbcTemplate jdbcTemplate() {
		return new JdbcTemplate(dataSource());
	}
}
